package com.yuanchu.store.controller;

import com.yuanchu.common.utils.PageResult;
import com.yuanchu.common.utils.R;
import com.yuanchu.common.validator.ValidationGroups;
import com.yuanchu.store.dto.EditAddressDto;
import com.yuanchu.store.dto.QueryPageParams;
import com.yuanchu.store.entity.YoshopStoreAddressEntity;
import com.yuanchu.store.service.YoshopStoreAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @version: 1.0
 * @Author: adan
 * @description:
 * @date: 2024-02-18 13:28
 */
@Api(value = "StoreAddressController", tags = {"商家地址"})
@RestController
@RequestMapping("address")
public class StoreAddressController {

    @Resource
    private YoshopStoreAddressService storeAddressService;
    Integer storeId = 10001;


    @ApiOperation("查询商家地址列表")
    @GetMapping("/list")
    public R queryStoreAddress(QueryPageParams params) {
        PageResult<YoshopStoreAddressEntity> list = storeAddressService.queryStoreAddress(params, storeId);
        return R.ok().putPage("data", list);
    }

    @ApiOperation("添加商家地址")
    @PostMapping("/add")
    public R addStoreAddress(@RequestBody @Validated({ValidationGroups.Insert.class}) EditAddressDto form) {
        boolean add = storeAddressService.addStoreAddress(form, storeId);
        if (!add) {
            return R.error("添加失败");
        }
        return R.ok();
    }

    @ApiOperation("编辑商家地址")
    @PostMapping("/edit")
    public R editStoreAddress(@RequestBody @Validated({ValidationGroups.Update.class}) EditAddressDto form) {
        boolean edit = storeAddressService.editStoreAddress(form, storeId);
        if (!edit) {
            return R.error("编辑失败");
        }
        return R.ok();
    }

    @ApiOperation("删除商家地址")
    @PostMapping("/delete")
    public R deleteStoreAddress(@RequestBody @Validated({ValidationGroups.Delete.class}) EditAddressDto form) {
        boolean delete = storeAddressService.deleteStoreAddress(form, storeId);
        if (!delete) {
            return R.error("删除失败");
        }
        return R.ok();
    }
    @ApiOperation("查询商家退货地址列表")
    @GetMapping("/all")
    public R queryStoreAddressAll(@RequestParam("type") Integer type) {
        List<YoshopStoreAddressEntity> list = storeAddressService.queryStoreAddressAll(type, storeId);
        return R.ok().put("data", list);
    }

    @ApiOperation("查询商家退货地址")
    @GetMapping("/info")
    public YoshopStoreAddressEntity queryStoreAddress(@RequestParam("addressId") Integer addressId) {
       return storeAddressService.getById(addressId);
    }



}
